package com.qiyu.wakaka.common.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.qiyu.wakaka.common.result.JsonResult;
import com.qiyu.wakaka.config.ImageOss;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.springframework.ui.ModelMap;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 文件上传工具
 * @Auther: bear
 * @Date: 2019/9/30 22:12
 * @Description:
 */
public class FileUploadUtils {

    /**
     * 视频处理：视频截帧生成封面图
     *
     * @author LH_Yu
     * @Param uploadFile 上传文件
     * @Param videoPath 原视频key
     * @Param coverPicturePath 封面图上传路径及取出url的key
     * "%s|sys/saveas,o_%s,b_%s" 具体处理格式化
     */
    public static String coverPicture(HttpServletRequest request, String name) {
        OSS ossClient = new OSSClientBuilder().build(ImageOss.endpoint, ImageOss.accessKeyId, ImageOss.accessKeySecret);
        try {
            if (ServletFileUpload.isMultipartContent(request)) {
                MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
                List<MultipartFile> fileList = multipartRequest.getFiles(name);
                if (fileList.size()==1) {
                    StringBuilder sbStyle = new StringBuilder();
                    StringBuilder sb = new StringBuilder();
                    sb.append(ImageOss.buildFileName(fileList.get(0)));
                    ossClient.putObject(ImageOss.bucketName, sb.toString(), fileList.get(0).getInputStream());
                    String url = ImageOss.BASE_URL+"/"+sb;
                    return url;
                }
            }
            return "";
        }catch (Exception e){
            return "";
        }finally {
            ossClient.shutdown();
        }
    }

    /**
     * 上传图片并返回图片url
     *
     * @return
     */
    public static String uploadImg(MultipartFile file, HttpServletRequest request) {
        OSS ossClient = new OSSClientBuilder().build(ImageOss.endpoint, ImageOss.accessKeyId, ImageOss.accessKeySecret);
        try {
            if (ossClient.doesBucketExist(ImageOss.bucketName)) {
            } else {
                ossClient.createBucket(ImageOss.bucketName);
            }
            StringBuilder sb = new StringBuilder();
            sb.append(ImageOss.buildFileName(file));

            ossClient.putObject(ImageOss.bucketName, sb.toString(), file.getInputStream());
            String s = ImageOss.BASE_URL + "/" +sb;
            return s;
        } catch (IOException e) {
            return "";
        } finally {
            ossClient.shutdown();
        }
    }


    /**
     * 可上传多图
     * @param request
     * @param name
     * @param file_id
     * @return
     */
    public static JsonResult uploadFiles(HttpServletRequest request, String name, Integer file_id) {
        StringBuilder sb = new StringBuilder();
        if (ServletFileUpload.isMultipartContent(request)) {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            List<MultipartFile> fileList;
            fileList = multipartRequest.getFiles(name);
            if (fileList.size()==0){
                fileList = multipartRequest.getFiles("file");
            }
            for (MultipartFile item : fileList) {
                sb.append(uploadImg(item,request)).append(",");
            }
            return new JsonResult().put("url",sb).put("name",name).put("file_id",file_id);
        }
        return JsonResult.error();
    }


    /**
     * 图片移除
     * @param request
     * @param response
     * @param model
     * @return
     */
    public static String deleteAssetsImage(HttpServletRequest request, HttpServletResponse response, ModelMap model) {

        response.setCharacterEncoding("UTF-8");
//        System.out.println("===========================");
//        System.out.println(request.getParameter("key"));
        return request.getParameter("key");

    }
}
